package org.weasel.controllers;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PlatformUI;
import org.weasel.preferences.GeneralPage;
import org.weasel.views.DeveloperEditView;
import org.weasel.views.IterationDetail;
import org.weasel.views.ProjectDetail;
import org.weasel.views.ReleaseDetail;
import org.weasel.views.UserStoryDetail;

public class PreferenceController {
	private static PreferenceController instance = null;
	private IPreferenceStore prefStore;
	private ApplicationController appController;
	private GeneralPage generalPage;
	//---------------------------------------------------------------------------------
	//preference constants - add corresponding one when new preferences are create
	//identifiers
	public static final String DATAFILELOC_PREFERENCE = "datafilelocation";
	public static final String TRADITIONALSTYLE_PREFERENCE = IWorkbenchPreferenceConstants.SHOW_TRADITIONAL_STYLE_TABS;
	public static final String MAXIMIZED_PREFERENCE = "startupMaximized";
	public static final String BURNDOWNDETAIL_PREFERENCE = "burndownDetail";
	
	//default values
	public static String DATAFILELOC_DEFAULT = System.getProperty("user.dir") + System.getProperty("file.separator") + "data.xml";
	public static final boolean TRADITIONALSTYLE_DEFAULT = false;
	public static final boolean MAXIMIZED_DEFAULT = true;
	public static final boolean BURNDOWNDETAIL_DEFAULT = false;
	//---------------------------------------------------------------------------------
	
	private PreferenceController(){
		//use the platform preference store
		prefStore = PlatformUI.getPreferenceStore();
	}
	
	public static PreferenceController instance(){
		if(instance==null)
			instance = new PreferenceController();
		return instance;
	}
	
	public IPreferenceStore getPrefStore() {
		return prefStore;
	}

	public void setGeneralPage(GeneralPage generalPage) {
		this.generalPage = generalPage;
	}

	public void setAppController(ApplicationController parentController) {
		this.appController = parentController;
	}

	/**
	 * Initializes the default values for all settings
	 * this is called at startup in the ApplicationWorkbenchWindowAdvisor class
	 *
	 */
	public void initDefaults(){
		prefStore.setDefault(TRADITIONALSTYLE_PREFERENCE,TRADITIONALSTYLE_DEFAULT);
		prefStore.setDefault(DATAFILELOC_PREFERENCE, DATAFILELOC_DEFAULT);
		prefStore.setDefault(MAXIMIZED_PREFERENCE, MAXIMIZED_DEFAULT);
		prefStore.setDefault(BURNDOWNDETAIL_PREFERENCE, BURNDOWNDETAIL_DEFAULT);
	}

	/**
	 * The following are the methods that are invoked by the preference-page to load/store the values
	 *
	 */
	
	public void restoreDefaults() {
		generalPage.getTxtDataFileLocation().setText(prefStore.getDefaultString(DATAFILELOC_PREFERENCE));
		generalPage.getCheckRoundTabs().setSelection(prefStore.getDefaultBoolean(TRADITIONALSTYLE_PREFERENCE));
		generalPage.getCheckMaximized().setSelection(prefStore.getDefaultBoolean(MAXIMIZED_PREFERENCE));
		generalPage.getCheckBurndown().setSelection(prefStore.getDefaultBoolean(BURNDOWNDETAIL_PREFERENCE));
	}

	public void loadPageValues() {
		generalPage.getTxtDataFileLocation().setText(prefStore.getString(DATAFILELOC_PREFERENCE));
		generalPage.getCheckRoundTabs().setSelection(prefStore.getBoolean(TRADITIONALSTYLE_PREFERENCE));
		generalPage.getCheckMaximized().setSelection(prefStore.getBoolean(MAXIMIZED_PREFERENCE));
		generalPage.getCheckBurndown().setSelection(prefStore.getBoolean(BURNDOWNDETAIL_PREFERENCE));
	}

	public void savePreferences() {
		String dataFileLoc = generalPage.getTxtDataFileLocation().getText();
		if(!dataFileLoc.equals(prefStore.getString(PreferenceController.DATAFILELOC_PREFERENCE))){
//			if(XMLValidator.getInstance().isValidXMLFile(dataFileLoc)) {
				//path of xml file changed and valid xml file selected --> reload model
				boolean result = MessageDialog.openConfirm(null, "Load XML file", "Are You sure that You want to load the new XML file?");
				if (result) {
					prefStore.setValue(DATAFILELOC_PREFERENCE, dataFileLoc);
					appController.loadModel();
					appController.getTreeController().displayTree();
					appController.changesOccured();
					appController.updateViews();
					appController.getTreeController().disableSaveButton();
					//this code closes all the open views which are depend on the data of the xml file
					IViewReference[] views = PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.getViewReferences();
					for (IViewReference reference : views) {
						String viewID = reference.getId();
						if (viewID.equals(ReleaseDetail.ID)
								|| viewID.equals(UserStoryDetail.ID)
								|| viewID.equals(ProjectDetail.ID)
								|| viewID.equals(DeveloperEditView.ID)
								|| viewID.equals(IterationDetail.ID)) {
							IWorkbenchPage workBenchPage = PlatformUI
									.getWorkbench().getActiveWorkbenchWindow()
									.getActivePage();
							workBenchPage.hideView(workBenchPage.findView(viewID));
						}
					}
				}
//			} else {
//				MessageDialog.openError(null, "Error while trying to load data from file", "The specified file is not a valid XML file for this application\nor the XML file is damaged!");
//			}
		}
		boolean traditionalStyle = generalPage.getCheckRoundTabs().getSelection();
		boolean maximizedWindow = generalPage.getCheckMaximized().getSelection();
		boolean burndownDet = generalPage.getCheckBurndown().getSelection();
		
		prefStore.setValue(TRADITIONALSTYLE_PREFERENCE, traditionalStyle);
		prefStore.setValue(MAXIMIZED_PREFERENCE, maximizedWindow);
		prefStore.setValue(BURNDOWNDETAIL_PREFERENCE, burndownDet);
	}
	
	public void selectXMLFilePath() {
		FileDialog fdialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
		fdialog.setFilterExtensions(new String[] {"*.xml"});
		fdialog.setFilterPath("c:\\");
		String path = fdialog.open();
	    if(path!=null)
	    	generalPage.getTxtDataFileLocation().setText(path);
	}
}
